#pragma once
#ifndef _DAO_H_
#define _DAO_H_
#ifdef WIN32
#pragma execution_character_set("utf-8")
#endif // WIN32
#include <QtSql/qsqldatabase.h>
#include <QtSql/qsqlquery.h>
#include <QtSql/qsqlerror.h>
#include <qsqltablemodel.h>
#include <QDateTime>
#include <QDebug>
#include "common.h"
#include "idProducer.h"
#include "utils.h"


/// <summary>
/// 数据库操作对象
/// </summary>
class Dao: public QObject {

	Q_OBJECT

public:

	Dao(QObject * parent = NULL);
	
	/// <summary>
	/// 初始化数据库环境
	/// </summary>
	/// <returns></returns>
	static bool initDatabase(Configuration::DataBaseConfig& config = Configuration::getInstance().database);

	/// <summary>
	/// 打开数据库，若数据库已经被打开，则返回true
	/// </summary>
	/// <param name="config"></param>
	/// <param name="databaseName"></param>
	/// <returns></returns>
	bool openDb(QString connectName = "Main", Configuration::DataBaseConfig& config = Configuration::getInstance().database);

	/// <summary>
	/// 判断数据库是否已经连接
	/// </summary>
	/// <returns></returns>
	bool isOpened();

	/// <summary>
	/// 添加检测参数（牌号/检测员/班次）
	/// </summary>
	/// <param name="paramName">数据类型，即数据库中操作的表的名称</param>
	/// <param name="name">名称</param>
	void addTestParam(const QString& tableName, const QString& name);

	/// <summary>
	/// 查询检测参数（牌号/检测员/班次）
	/// </summary>
	/// <param name="paramName">数据类型，即数据库中操作的表的名称</param>
	/// <param name="names">输出查询到的所有名称</param>
	void queryTestParam(const QString& tableName, QStringList& names);

	/// <summary>
	/// 查询历史检测记录中使用过的检测参数名称（牌号，检测员，班次）
	/// </summary>
	/// <param name="brands">输出牌号</param>
	/// <param name="testers">输出检测员</param>
	/// <param name="schedules">输出班次</param>
	void queryHistoryTestParams(QStringList& testers, QStringList& schedules);

	/// <summary>
	/// 删除检测参数（牌号/检测员/班次）
	/// </summary>
	/// <param name="paramName">数据类型，即数据库中操作的表的名称</param>
	/// <param name="name">名称</param>
	void deleteTestParam(const QString& tableName, const QString& name);

	/// <summary>
	/// 更新检测参数的最新使用时间
	/// </summary>
	/// <param name="paramName"></param>
	/// <param name="name"></param>
	void updateTestParamLastUseTime(const QString& tableName, const QString& name);


	/// <summary>
	/// 添加检测数据
	/// </summary>
	/// <param name="data"></param>
	void addTestData(DaoModel::TestData& data);

	/// <summary>
	/// 通过检测编号删除测量记录
	/// </summary>
	/// <param name="id">检测记录的主键</param>
	void deleteRecord(qint64 id);

	/// <summary>
	/// 批量删除检测记录
	/// </summary>
	/// <param name="recordIds">检测记录</param>
	void deleteRecords(vector<qint64>& recordIds);

	/// <summary>
	/// 删除指定条件的所有记录
	/// </summary>
	/// <param name="records"></param>
	/// <param name="beginDay"></param>
	/// <param name="endDay"></param>
	/// <param name="tester"></param>
	/// <param name="schedule"></param>
	void deleteRecords(QDate& beginDay, QDate& endDay, QString tester, QString schedule);

	/// <summary>
	/// 查询指定日期的测量记录的时间和牌号
	/// </summary>
	/// <param name="records">查询结果</param>
	/// <param name="beginDay">开始日期</param>
	/// <param name="endDay">结束日期</param>
	/// <param name="tester">检测员</param>
	/// <param name="schedule">班次</param>
	void queryTestRecords(QList<DaoModel::TestData>& records, QDate& beginDay, QDate& endDay, QString tester, QString schedule);
	

	/// <summary>
	/// 通过检测记录查询当前记录下的所有数据
	/// </summary>
	/// <param name="dataList"></param>
	/// <param name="createTime">检测时间</param>
	void queryTestData(QList<DaoModel::TestData>& dataList, QString createTime);


private:

	QString connectName;

	QSqlDatabase database;
};

#endif